﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneAppCheckSensors
{
    using System.Device.Location;

    public partial class PageLocation : PhoneApplicationPage
    {
        public PageLocation()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (myWatcher == null)
            {
                MessageBox.Show(
@"This page show your current location raw data got from 'Location API'.
This application show location raw data only and doesn't share the data for external of your device.");
                myWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                myWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(myWatcher_StatusChanged);
                myWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(myWatcher_PositionChanged);
            }
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (buttonStop.IsEnabled)
            {
                myWatcher.Stop();
                buttonStart.IsEnabled = true;
                buttonStop.IsEnabled = false;
            }

            base.OnNavigatedFrom(e);
        }

        private GeoCoordinateWatcher myWatcher;

        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            tbStatus.Text = myWatcher.Status.ToString();
            
            buttonStart.IsEnabled = false;
            buttonStop.IsEnabled = true;
            myWatcher.Start();
        }

        void myWatcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                tbStatus.Text = e.Status.ToString();
            });
        }

        void myWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                tbTimestamp.Text = e.Position.Timestamp.ToString();
                double altitude = e.Position.Location.Altitude;
                double course = e.Position.Location.Course;
                double horizontalAccuracy = e.Position.Location.HorizontalAccuracy;
                double verticalAccuracy = e.Position.Location.VerticalAccuracy;
                double latitude = e.Position.Location.Latitude;
                double longitude = e.Position.Location.Longitude;
                double speed = e.Position.Location.Speed;

                tbAltitude.Text = String.Format("{0:0.0000}", altitude);
                tbCourse.Text = String.Format("{0:0.0000}", course);
                tbLatitude.Text = String.Format("{0:0.0000}", latitude);
                tbLongitude.Text = String.Format("{0:0.0000}", longitude);
                tbSpeed.Text = String.Format("{0:0.0000}", speed);
                tbAccuracy.Text = String.Format("Horizontal={0:0.0000},Vertical={1:0.0000}", horizontalAccuracy, verticalAccuracy);

            });
        }

        private void buttonStop_Click(object sender, RoutedEventArgs e)
        {
            myWatcher.Stop();
            buttonStop.IsEnabled = false;
            buttonStart.IsEnabled = true;
        }
    }
}